package com.rose.doorto.footbathdoor.interceptor;

import com.rose.doorto.common.constant.AuthServerConstant;
import com.rose.doorto.footbathdoor.pojo.User;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.footbathdoor.interceptor
 * @Project：door-to-door
 * @name：LoginInterceptor
 * @date 2024/08/12 2024/8/12:18:37:
 */
public class LoginInterceptor implements HandlerInterceptor {

    public static final ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 目标方法请求到来之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        //有些远程调用不需要登录,直接放行,  TODO 目前写死,全部放行
//        boolean match = new AntPathMatcher().match("/api/**", uri);
        boolean match = new AntPathMatcher().match("/**", uri);
        // 支付宝异步通知不需要拦截
        boolean match1 = new AntPathMatcher().match("/alipay/notify", uri);
        if (match || match1) {
            return true;
        }
        HttpSession session = request.getSession();
        User attribute = (User) session.getAttribute(AuthServerConstant.LOGIN_USER_KEY);
        if (attribute == null){
            //用户未登录
            response.sendRedirect("login.html");
            return false;
        }else {
            //已登录,用threadLocal共享数据
            threadLocal.set(attribute);
            return true;
        }

















    }
}
